import type { Result } from "#/api";
import type { Role } from "#/entity";
import apiClient from "../../apiClient";

export interface PageListReq {
	code?: string;
	name?: string;
	status?: string;
	page: number;
	pageSize: number;
}

export interface PageListRes {
	total: number;
	records: Role[];
}

export interface RoleMenu {
	roleId: string;
	sourceIds: React.Key[];
}

interface RoleUser {
	roleId: string;
	userIds: (number | string)[];
}

const findById = (id: string) => apiClient.get<Role>({ url: `/sys/role/getRole/${id}` });
const pageList = (data: PageListReq) => apiClient.post<PageListRes>({ url: "/sys/role/list", data });
const add = (data: Role) => apiClient.post<Role>({ url: "/sys/role/add", data });
const update = (data: Role) => apiClient.post<Role>({ url: "/sys/role/editRole", data });
const deleteRole = (ids: string[]) => apiClient.delete({ url: "/sys/role/del", params: { ids: ids.join(",") } });

const updateRoleUser = (data: RoleUser) => apiClient.post<Result<any>>({ url: "/sys/roleUser/updateRoleMenu", data });
const updateRoleMenu = (data: RoleMenu) => apiClient.post<Result<any>>({ url: "/sys/roleMenu/updateRoleMenu", data });

const deleteRoleUser = (data: RoleUser) => apiClient.post<Result<any>>({ url: "/sys/roleUser/del", data });

export default {
	findById,
	pageList,
	add,
	update,
	deleteRole,
	updateRoleUser,
	updateRoleMenu,
	deleteRoleUser,
};
